import { _decorator, Component, Label, Node, RigidBody2D, sp, Sprite } from 'cc';
import { LayerBase } from './LayerBase';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { UserInfo } from './UserInfo';
import { randomNum } from '../utils/utils';
import { UILayerMgr } from './UILayerMgr';
import { gameMgr } from '../mainScene/gameMgr';
import { rewardEnum } from '../Enum';
import { Adsdk } from '../Adsdk';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('TurntablePanel')
export class TurntablePanel extends LayerBase {
   
    @property(Label)
    spaceNumLabel:Label
    @property(Node)
    tablebg:Node
    @property(Node)
    topMask:Node;
    @property(sp.Skeleton)
    turnTableSp:sp.Skeleton;
    @property(Node)
    freeBtn:Node;
    @property(Node)
    seeAdvBtn:Node;
    

    init(somedData?: any): void {
        this.turnTableSp.setAnimation(0,"show",true)
        this.spaceNumLabel.getComponent(Label).string=UserInfo.drawTurnTableNum+"/"+StartSceneGameMgr.ins.drawTurntableTotalNum;

        if(UserInfo.drawTurnTableNum==-1){
            this.freeBtn.active=true;
            this.seeAdvBtn.active=false;
        }
        else{
            this.seeAdvBtn.active=true;
            this.freeBtn.active=false;
        }
    }

    onclickTurntableBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (UserInfo.drawTurnTableNum >= StartSceneGameMgr.ins.drawTurntableTotalNum) {
            StartSceneGameMgr.ins.createWidowTip("今日次数用完了，明天再来吧")
        }
        else {
            if (UserInfo.drawTurnTableNum==-1) {//免费抽轮盘
                console.log("免费抽轮盘");
              this.drawTurntable();
            }
            else{
                Adsdk.instance.showVideo(()=>{
                    this.drawTurntable();
                  },()=>{})
            }
        }
    }

    //播放转盘动画
    playAniTurntableByRewardIndex(index) {
        console.log("播发动画")
        switch (index) {
            case 0:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(78000, 82000), true);
                break;
            case 1:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(82500, 86500), true);
                break;
            case 2:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(88000, 92000), true);
                break;
            case 3:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(92500, 96500), true);
                break;
            case 4:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(97500, 101500), true);
                break;
            case 5:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(102500, 106500), true);
                break;
            case 6:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(107500, 111500), true);
                break;
            case 7:
                this.tablebg.getComponent(RigidBody2D).applyAngularImpulse(randomNum(112500, 116500), true);
                break;

        }
    }

    drawTurntable(){
        this.turnTableSp.setAnimation(0,"stand_2",true)
        this.tablebg.angle = 0;
        UserInfo.drawTurnTableNum += 1;
        let rewardArr = [0, 1, 2, 3, 4, 5, 6, 7];

        let randCount = randomNum(1, 100);
        let rewardIndex=0;;
        if (0 < randCount && randCount < 25) {//25%
            rewardIndex = 0
        }
        else if (25 <= randCount && randCount < 45) {//20%
            rewardIndex = 1
        }
        else if (45 <= randCount && randCount < 60) {//15%
            rewardIndex = 2
        }
        else if (60 <= randCount && randCount < 75) {//15%
            rewardIndex = 3
        }
        else if (75 <= randCount && randCount < 85) {//10%
            rewardIndex = 4
        }
        else if (85 <= randCount && randCount < 93) {//8%
            rewardIndex = 5
        }
        else if (93 <= randCount && randCount < 98) {//5%
            rewardIndex = 6
        }
        else if (98 <= randCount && randCount <= 100) {//2%
            rewardIndex = 7
        }
     

        this.playAniTurntableByRewardIndex(rewardIndex);
        this.topMask.active = true;

        setTimeout(() => {
            switch (rewardIndex) {
                case 0:
                    this.getCrystal(10);
                    break;
                case 1:
                    this.getCrystal(30);
                    break;
                case 2:
                    this.getCrystal(50);
                    break;
                case 3:
                    this.getCoin(80);
                    break;
                case 4:
                    this.getRandomEndSkillProp(1);
                    break;
                case 5:
                    this.getRandomEquip();
                    break;
                case 6:
                    this.getEpicBoxKey(1);
                    break;
                case 7:
                    this.getLegendBoxKey(1);
                    break;
            }

            this.topMask.active = false;
            StartSceneGameMgr.ins.saveGameDate();
            UILayerMgr.getIns.resfreshUI(this.node.name);
            this.turnTableSp.setAnimation(0, "show", true)
        }, 5000)
    }

    getRandomEquip() {
        let randCount = randomNum(1, 100);
        let getQualityType = 0;

        if (0 < randCount && randCount < 60) {//普通,60%
            getQualityType = 0
        }
        else if (60 <= randCount && randCount < 85) {//稀有,25%
            getQualityType = 1
        }
        else if (85 <= randCount && randCount < 98) {//史诗,13%
            getQualityType = 2
        }
        else if (98 <= randCount && randCount <= 100) {//传说,2%
            getQualityType = 3
        }

        let qualityEquipCongfiArr = [];
        UserInfo.equipCongfiDic.getValueList().forEach(element => {
            if (element.quality == getQualityType) {
                qualityEquipCongfiArr.push(element)
            }
        });

        let getEquipCongfi = qualityEquipCongfiArr[randomNum(0, qualityEquipCongfiArr.length - 1)];

        StartSceneGameMgr.ins.getEquip(getEquipCongfi.equipType);
        getEquipCongfi.count=1;
        UILayerMgr.getIns.openUILayer("getRewardPanel",getEquipCongfi);
    }

    getCrystal(Num) {
        UserInfo.crystalMun+=Num;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.crystal);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

    getCoin(Num) {
        UserInfo.coinMun+=Num;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.coin);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

    //得到史诗宝箱钥匙
    getEpicBoxKey(Num) {
        UserInfo.epicBoxKeyNum += Num;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.epicBoxKey);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

    getLegendBoxKey(Num) {
        UserInfo.legendBoxKeyNum += Num;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.legendBoxKey);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

    //获得随机道具卡
    getRandomEndSkillProp(Num){
        let randCount = randomNum(1, 100);
        let getTypeEndSkill = 0;

        if (0 < randCount && randCount < 40) {//40%
            getTypeEndSkill = 0
        }
        else if (40 <= randCount && randCount < 70) {//30%
            getTypeEndSkill = 1
        }
        else if (70 <= randCount && randCount <= 100) {//30%
            getTypeEndSkill = 2
        }

        UserInfo.hasEndSkillPropArr[getTypeEndSkill]+=Num;

        let rewardCongfi=UserInfo.rewardCongfiDic.get(getTypeEndSkill+3);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
       
    }
}


